/*
   The contents of this file are subject to the Mozilla Public License
   Version 1.1 (the "License"); you may not use this file except in
   compliance with the License. You may obtain a copy of the License at
   http://www.mozilla.org/MPL/

   Software distributed under the License is distributed on an "AS IS"
   basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
   License for the specific language governing rights and limitations
   under the License.

   The Original Code is: www.iwobanas.com code samples.

   The Initial Developer of the Original Code is Iwo Banas.
   Portions created by the Initial Developer are Copyright (C) 2009
   the Initial Developer. All Rights Reserved.

   Contributor(s):
 */
package vivek.project.pocketmoney.common.controls
{
	import flash.events.MouseEvent;
	import spark.components.Button;
	import spark.components.Panel;

	/**
	 * The CollapsiblePanel class adds support for collapsing (minimizing) to the Spark Panel.
	 */
	[SkinState("collapse")]
	public class ModulePanel extends Panel
	{
		[SkinPart(required="false")]
		/**
		 *  The skin part that defines the appearance of the
		 *  button responsible for collapsing/uncollapsing the panel.
		 */
		public var collapseButton:Button;
		[SkinPart(required="false")]
		public var exporterButton:Button;
		private var _helpControlId:String;
		private var _moduleId:String;
		private var _exportFunction:Function=new Function();
		private var _showExport:Boolean=false;
		public function get moduleId():String
		{
			return _moduleId;
		}
		protected var _collapsed:Boolean;
		public function set moduleId(value:String):void
		{
			_moduleId=value;
		}
		public function get showExport():Boolean
		{
			return _showExport;
		}
		public function set showExport(value:Boolean):void
		{
			_showExport=value;
		}
		public function get exportFunction():Function
		{
			return _exportFunction;
		}
		public function set exportFunction(value:Function):void
		{
			_exportFunction=value;
			exporterButton.addEventListener(MouseEvent.CLICK, _exportFunction);
		}
		public function get helpControlId():String
		{
			return _helpControlId;
		}
		public function set helpControlId(value:String):void
		{
			_helpControlId=value;
		}
		protected function collapseButtonClickHandler(event:MouseEvent):void
		{
			collapsed=!collapsed;
		}
		public function set collapsed(value:Boolean):void
		{
			_collapsed=value;
			invalidateSkinState();
		}
		public function get collapsed():Boolean
		{
			return _collapsed;
		}
		override protected function partAdded(partName:String, instance:Object):void
		{
			super.partAdded(partName, instance);
			if (instance == exporterButton)
				Button(instance).addEventListener(MouseEvent.CLICK, _exportFunction);
			if (instance == collapseButton)
			{
				Button(instance).addEventListener(MouseEvent.CLICK, collapseButtonClickHandler);
			}
		}
		override protected function getCurrentSkinState():String
		{
			return collapsed ? "collapse" : super.getCurrentSkinState();
		}
	}
}